﻿using DramaEnglish.WPF.ViewModels;
using Open.EMR.UIElements;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Open.EMR.ViewModels.Edition
{
    public class DesignerViewModel : ViewModelBase
    {

        #region Fields Properties
        BlodLabel blodLabel;
        RichTextBox richTextBox;
        #endregion

        #region Constructors
        public DesignerViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator ea, BlodLabel blodLabel)
        : base(regionManager, dialogService, ea)
        {
            this.blodLabel = blodLabel;
            EventAggregator.GetEvent<PubSubEvent<EnumElement>>().Subscribe((x) =>
            {
                blodLabel.Add2Doc(richTextBox);
            });
        }
        #endregion

        #region Cammands

        public DelegateCommand<RichTextBox> RTBLoadedCommand => new((x) =>
        {
            richTextBox = x;
        });
        public DelegateCommand demoCommand => new(() =>
        {
            blodLabel.Add2Doc(richTextBox);
        });

        public DelegateCommand SaveCommand => new(() =>
        {
            string filename = @"C:\Users\Lenovo\Desktop\a.xaml";
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException();
            }
            using (FileStream stream = File.OpenWrite(filename))
            {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml", true) == 0)
                {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0)
                {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Save(stream, dataFormat);
            }
        });


        #endregion

        #region Functions Methods

        #endregion

    }
}
